﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using BLL;

namespace TaoBaoProject.Handlers.MenbersHandler
{
    /// <summary>
    /// Summary description for UpdateMenbersHandler
    /// </summary>
    public class UpdateMenbersHandler : IHttpHandler
    {
        MenbersBLL menbersBLL = new MenbersBLL();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int isSameQQAndName = menbersBLL.GetMenbers(context.Request["QQ"].ToString(), context.Request["Name"].ToString());
            if (isSameQQAndName == 0)
            {
                string menberID = context.Request.QueryString["id"];
                string carFront = null;
                string carBack = null;
                string ph = null;
                SaveAllFiles(context.Request.Files, ref carFront, ref carBack, ref ph);
                Menbers menbers = new Menbers();

                Menbers ImgsMenbers = menbersBLL.GetListMenbers(menberID);


                if (carFront == null)
                    menbers.IdentityCardFront = ImgsMenbers.IdentityCardFront == null ? " " : ImgsMenbers.IdentityCardFront;
                else
                    menbers.IdentityCardFront = carFront == null ? " " : carFront;

                if (carBack == null)
                    menbers.IdentityCardBack = ImgsMenbers.IdentityCardBack == null ? " " : ImgsMenbers.IdentityCardBack;
                else
                    menbers.IdentityCardBack = carBack == null ? " " : carBack;
                if (ph == null)
                    menbers.Photo = ImgsMenbers.Photo == null ? " " : ImgsMenbers.Photo;
                else
                    menbers.Photo = ph == null ? " " : ph;

                menbers.MenberID = Convert.ToInt32(menberID);
                menbers.QQ = context.Request["QQ"].ToString();
                menbers.Name = context.Request["Name"].ToString();
                menbers.IdentityCardAddress = context.Request["IdentityCardAddress"].ToString();
                menbers.ZhifubaoAccountName = context.Request["ZhifubaoAccountName"].ToString();
                menbersBLL.UpdateMenbers(menbers);
                string returnValue = "{QQ:'" + menbers.QQ + "',Name:'" + menbers.Name + "',IdentityCardAddress:'" + menbers.IdentityCardAddress + "',IdentityCardFront:'" + menbers.IdentityCardFront + "',IdentityCardBack:'" + menbers.IdentityCardBack + "',ZhifubaoAccountName:'" + menbers.ZhifubaoAccountName + "',Photo:'" + menbers.Photo + "'}";
                context.Response.Write(returnValue);
            }
            else
            {
                context.Response.Write("{Repeater:'1'}");
            }

        }

        private void SaveAllFiles(HttpFileCollection imgs, ref string carFront, ref string carBack, ref string ph)
        {
            try
            {
                if (imgs.Count > 0)
                {
                    for (int i = 0; i < imgs.Count; i++)
                    {
                        if (imgs[i].ContentLength != 0)
                        {
                            string oldName = imgs[i].FileName.ToString();
                            string imgName = Guid.NewGuid().ToString();
                            string imgType = oldName.ToString().Substring(oldName.LastIndexOf("."));
                            string imgPath = "~/UploadImgs/" + imgName + imgType;
                            imgs[i].SaveAs(HttpContext.Current.Server.MapPath(imgPath));
                            if (HttpContext.Current.Request.Files["IdentityCardFront"].FileName == imgs[i].FileName)
                                carFront = imgName + imgType;
                            if (HttpContext.Current.Request.Files["IdentityCardBack"].FileName == imgs[i].FileName)
                                carBack = imgName + imgType;
                            if (HttpContext.Current.Request.Files["Photo"].FileName == imgs[i].FileName)
                                ph = imgName + imgType;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}